import axios from "axios";
import { ElMessageBox } from 'element-plus';

const instance = axios.create({
    baseURL: import.meta.env.VITE_APP_API,
    timeout: 10000,
    accept: 'application/json',
    headers: {
        Authorization: "Bearer " + localStorage.getItem('token')
    }
});

instance.interceptors.response.use(function (response) {
    return response.data;
}, function (error) {
    if (error.status == 401) {
        ElMessageBox.alert(
            '登录状态失效!',
            'Warning',
            {
                confirmButtonText: '确定',
                type: 'warning',
            }
        ).then(() => {
            localStorage.removeItem('token')
        })
    }
    return Promise.reject(error);
});

export default instance;